package com.jiangnan.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.jiangnan.R
import com.jiangnan.entity.Book
import com.jiangnan.databinding.ItemBookRepositoryBinding
import com.jiangnan.util.GlideUtil

class RepositoryQuickAdapter : BaseQuickAdapter<Book, RepositoryQuickAdapter.VH>() {

    // 自定义ViewHolder类
    class VH(
        parent: ViewGroup,
        val binding: ItemBookRepositoryBinding = ItemBookRepositoryBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        // 返回一个 ViewHolder
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: Book?) {
        // 设置item数据
        item.let {
            GlideUtil.loadImageWithPlaceholder(
                holder.binding.ivBookImg.context,
                it?.img ?: "",
                R.drawable.img_default,
                R.drawable.img_error,
                holder.binding.ivBookImg
            )
            holder.binding.tvBookTitle.text = it?.title
            holder.binding.tvBookTitle.isSelected = true
            holder.binding.tvBookAuthor.text = it?.author
            holder.binding.tvBookPrice.text = "" + it?.price
            var lastAndSum = "" + it?.lastNum + "/" + it?.sum + "本"
            holder.binding.tvBookLastSum.text = lastAndSum
        }
    }

}